using System;
using System.Collections;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.ApplicationBlocks.Data;

using LocalBoard.Domain;

namespace LocalBoard.Persistence.SqlAccessor
{
	/// <summary>
	/// Summary description for SqlClubAccessor.
	/// </summary>
	public class SqlClubAccessor : SqlDataAccessBase, IClubAccessor
	{
		public static readonly SqlClubAccessor Instance = new SqlClubAccessor();
		private SqlAccessorFactory _accessorFactory;
		private IClubRoleAccessor _clubRoleAccessor;

		private SqlClubAccessor()
		{
			_accessorFactory = new SqlAccessorFactory();
			_clubRoleAccessor = _accessorFactory.GetClubRoleAccessor();
		}

		#region IClubAccessor Members

		public ArrayList GetClubList()
		{
			SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetClubList");

			ArrayList clubList = new ArrayList();

			while(dataReader.Read())
			{
				clubList.Add(GetClubFromResultRow(dataReader));
			}
			dataReader.Close();
			return clubList;
		}

		public Club Get(int id)
		{
			SqlDataReader dataReader = null;
			try
			{
				dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetClubById", id);
				// Execute the stored procedure
				if(dataReader.Read())
					return GetClubFromResultRow(dataReader);
			}
			catch (Exception ex )
			{
				// Log error details
				throw ex;
			}
			finally
			{
				if(dataReader != null) dataReader.Close();
			}
			return null;
		}

		public int Insert(Club club)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "InsertClub", club.UniqueName, club.Name, club.CategoryId, club.Description, club.ClubType.ToString(), club.Information != null ? club.Information.OuterXml : ""));
		}

		public void Update(Club club)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "UpdateClub", club.Id, club.UniqueName, club.Name, club.CategoryId, club.Description, club.ClubType.ToString(), club.Information != null ? club.Information.OuterXml : "");
		}

		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "DeleteClub", id);
		}

		public void AssignManagerToClub(int profileId, int id)
		{
			UserRole clubManagerRole = _clubRoleAccessor.Get(UserRole.CLUB_MANAGER_ROLE_NAME, id);
			if(clubManagerRole == null)
				clubManagerRole = _clubRoleAccessor.Get(_clubRoleAccessor.Insert(UserRole.CLUB_MANAGER_ROLE_NAME, id), id);
			_clubRoleAccessor.AddClubRoleToProfile(clubManagerRole.Id, profileId, id);
		}

		public ArrayList GetClubListByKeyword(string keyword)
		{
			SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "SearchClub", keyword);

			ArrayList clubList = new ArrayList();

			while(dataReader.Read())
			{
				clubList.Add(GetClubFromResultRow(dataReader));
			}
			dataReader.Close();
			return clubList;
		}
		#endregion

		private Club GetClubFromResultRow(SqlDataReader dataReader)
		{
			return new Club(DataConverter.GetInteger(dataReader["Id"]), DataConverter.GetString(dataReader["UniqueName"]), DataConverter.GetString(dataReader["Name"]), DataConverter.GetInteger(dataReader["Category_Id"]), DataConverter.GetString(dataReader["Description"]), DataConverter.GetString(dataReader["ClubType"]), GetXmlNodeFromXmlString(DataConverter.GetString(dataReader["Information"])));
		}

		private XmlNode GetXmlNodeFromXmlString(string xmlString)
		{
			if(xmlString == null || xmlString.Length == 0)
				return null;
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(xmlString);
			return doc;
		}
	}
}
